/**
 * File Comment: Ita a GUI
 */
package edu.gatech.oad.project.presentation;
//$codepro.audit.disable
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

/**
 * This class is a panel that allows the user
 * to interact with the GUI to view a list of
 * existing patients in the system.
 * 
 * @author Group 17
 * @version 1
 */
public class NursePatientList extends JPanel {
  
  /**
   * Field driver: holds the Driver connected to the GUI
   */
  private Driver driver;
  
  /**
   * Field textField: search field
   */
 private JTextField textField;
 
 /**
  * Field list: list of patients
  */
 public JList list;
 
 /**
  * Field btnEditPatient: edit patient button
  */
 public JButton btnEditPatient;
 
 /**
  * Field btnBackToHome: back button
  */
 public JButton btnBackToHome;
 
 /**
  * Field name: name of patient
  */
 public String name;

 /**
  * Constructor.
  * 
  * @param driver the driver connected to the GUI
  */
 public NursePatientList(Driver driver) {
   this.driver = driver;
  GridBagLayout gridBagLayout = new GridBagLayout();
  gridBagLayout.columnWidths = new int[]{0, 0, 0, 0, 0};
  gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
  gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0, 0.0, Double.MIN_VALUE};
  gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
  setLayout(gridBagLayout);
  
  JLabel lblPatients = new JLabel("View/Edit Patients");
  lblPatients.setFont(new Font("Lucida Grande", Font.PLAIN, 30));
  GridBagConstraints gbc_lblPatients = new GridBagConstraints();
  gbc_lblPatients.gridwidth = 4;
  gbc_lblPatients.insets = new Insets(0, 0, 5, 0);
  gbc_lblPatients.gridx = 0;
  gbc_lblPatients.gridy = 1;
  add(lblPatients, gbc_lblPatients);
  
  JLabel lblPatients_1 = new JLabel("Patients");
  GridBagConstraints gbc_lblPatients_1 = new GridBagConstraints();
  gbc_lblPatients_1.insets = new Insets(0, 0, 5, 5);
  gbc_lblPatients_1.gridx = 0;
  gbc_lblPatients_1.gridy = 3;
  add(lblPatients_1, gbc_lblPatients_1);
  
  list = new JList();
  GridBagConstraints gbc_list = new GridBagConstraints();
  gbc_list.gridwidth = 2;
  gbc_list.insets = new Insets(0, 0, 5, 0);
  gbc_list.gridheight = 4;
  gbc_list.fill = GridBagConstraints.BOTH;
  gbc_list.gridx = 1;
  gbc_list.gridy = 3;
  add(list, gbc_list);
  
  JLabel lblByName = new JLabel("By Name");
  GridBagConstraints gbc_lblByName = new GridBagConstraints();
  gbc_lblByName.anchor = GridBagConstraints.EAST;
  gbc_lblByName.insets = new Insets(0, 0, 5, 5);
  gbc_lblByName.gridx = 0;
  gbc_lblByName.gridy = 7;
  add(lblByName, gbc_lblByName);
  
  textField = new JTextField();
  textField.addCaretListener(new CaretListener() {
    public void caretUpdate(CaretEvent e) {
      name = (String)textField.getText();
      //write code to update list
    }
  });
  GridBagConstraints gbc_textField = new GridBagConstraints();
  gbc_textField.insets = new Insets(0, 0, 5, 5);
  gbc_textField.fill = GridBagConstraints.HORIZONTAL;
  gbc_textField.gridx = 1;
  gbc_textField.gridy = 7;
  add(textField, gbc_textField);
  textField.setColumns(10);
  
  btnEditPatient = new JButton("Edit Patient");
  btnEditPatient.addActionListener(new ButtonListener1(this));
  GridBagConstraints gbc_btnEditPatient = new GridBagConstraints();
  gbc_btnEditPatient.insets = new Insets(0, 0, 0, 5);
  gbc_btnEditPatient.gridx = 1;
  gbc_btnEditPatient.gridy = 8;
  add(btnEditPatient, gbc_btnEditPatient);
  
  btnBackToHome = new JButton("Back to Home");
  btnBackToHome.addActionListener(new ButtonListener2(this));
  GridBagConstraints gbc_btnBackToHome = new GridBagConstraints();
  gbc_btnBackToHome.insets = new Insets(0, 0, 0, 5);
  gbc_btnBackToHome.gridx = 2;
  gbc_btnBackToHome.gridy = 8;
  add(btnBackToHome, gbc_btnBackToHome);
 } // end constructor method

 /**
  * Get the Driver.
  * 
  * @return the driver connected to the GUI
  */
 public Driver getDriver()
 {
   return this.driver;
 } // end getDriver method
 
 /**
  * Get the selected patient.
  * 
  * @return the name of the selected patient
  */
 public String getSelected()
 {
   return (String)this.list.getSelectedValue();
 } // end getSelected method
 
 /**
  * This private inner class implements ActionListener for btnEditPatient.
  * 
  * @author Group 17
  * @version 1
  */
 private class ButtonListener1 implements ActionListener
 {
   /**
    * Field pl: the NursePatientList being used
    */
   private NursePatientList pl;
   
   /**
    * Field driver: holds the Driver connected to the GUI
    */
   private Driver driver;
   
   /**
    * Constructor.
    * 
    * @param pl the NursePatientList being used
    */
   private ButtonListener1(NursePatientList pl)
   {
     this.pl = pl;
     this.driver = pl.getDriver();
   } // end constructor method
   
   /**
    * Default method for ActionListener.
    * 
    * @param e the ActionEvent triggering the ActionListener
    */
   public void actionPerformed(ActionEvent e)
   {
     this.driver.getFrame().setContentPane(new NursePatientInfo(this.driver,this.pl.getSelected()));
     this.driver.getFrame().pack();
   } // end actionPerformed method
 } // end ButtonListener1 class
 
 /**
  * This private inner class implements ActionListener for btnBackToHome.
  * 
  * @author Group 17
  * @version 1
  */
  private class ButtonListener2 implements ActionListener
 {
    /**
     * Field pl: the NursePatientList being used
     */
   private NursePatientList pl;
   
   /**
    * Field driver: holds the Driver connected to the GUI
    */
   private Driver driver;
   
   /**
    * Constructor.
    * 
    * @param pl the NursePatientList being used
    */
   private ButtonListener2(NursePatientList pl)
   {
     this.pl = pl;
     this.driver = pl.getDriver();
   } // end constructor method
   
   /**
    * Default method for ActionListener.
    * 
    * @param e the ActionEvent triggering the ActionListener
    */
   public void actionPerformed(ActionEvent e)
   {
     this.driver.getFrame().setContentPane(new DoctorHome(this.driver));
     this.driver.getFrame().pack();
   } // end actionPerformed method
 } // end ButtonListener2 class
} // end NursePatientList class